<template>
  <div class="app-container">

    <u-page title="导购管理">

      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="类型" prop="type">
            <el-select v-model="listQuery.type" placeholder="请选择" clearable>
              <el-option value="" label="全部" />
              <el-option :value="0" label="正式员工" />
              <el-option :value="1" label="试用员工" />
            </el-select>
          </el-form-item>
          <el-form-item label="工号" prop="jobNo">
            <el-input v-model="listQuery.jobNo" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="姓名" prop="name">
            <el-input v-model="listQuery.name" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="listQuery.mobile" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="部门" prop="dept">
            <el-input v-model="listQuery.dept" placeholder="关键词搜索" clearable />
          </el-form-item>
          <el-form-item label="公司" prop="enterpriseName">
            <el-input v-model="listQuery.enterpriseName" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="商户" prop="merchantName">
            <el-input v-model="listQuery.merchantName" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="楼层" prop="floor">
            <mall-floor v-model="listQuery.floor" />
          </el-form-item>

          <el-form-item label="状态" prop="status">
            <el-select v-model="listQuery.status" placeholder="请选择状态" class="status" clearable>
              <el-option value="" label="全部" />
              <el-option :value="0" label="待入职" />
              <el-option :value="1" label="正常" />
              <el-option :value="2" label="离职" />
              <el-option :value="3" label="锁定" />
            </el-select>
          </el-form-item>
          <el-form-item label="入职时间">
            <u-date-time-picker v-model="searchDate" @change="changeSearchDate" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
          </el-form-item>
          <el-form-item v-if="!purview">
            <el-dropdown>
              <el-button type="primary">
                批量操作<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :disabled="selectData.length===0" @click.native="hdlDel">批量删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
          <el-form-item>
            <el-button v-if="!purview" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button v-if="row.status === 0 && !purview" type="text" @click="hdlApproval(row)">入职确认</el-button>
              <el-button type="text" @click="hdlDetail(row)">详情</el-button>
              <el-button v-if="row.status !== 0 && !purview" type="text" @click="hdlEdit(row)">编辑</el-button>
              <el-button v-if="!purview" type="text" @click="hdlSync(row)">同步</el-button>
              <el-button v-if="row.type === 1" type="text" @click="hdRegularize(row)">转正</el-button>
              <el-dropdown v-if="row.status !== 0" style="margin-left: 10px;">
                <el-button type="text">更多</el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="hdlBadge(row)">工牌生成</el-dropdown-item>
                  <el-dropdown-item v-if="row.type !== 0" @click.native="hdlEdit2(row)">更改日期</el-dropdown-item>
                  <el-dropdown-item v-if="row.type === 0" @click.native="hdlUpdate(row)">更改餐费</el-dropdown-item>
                  <el-dropdown-item v-if="row.type === 0" @click.native="hdlSingleMeal(row)">餐费充值</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </template>
      </u-table>

    </u-page>

    <modal v-model="dialogFormVisible" :data="dataForm" />
    <minorModal v-model="dialogMinorFormVisible" :data="dataForm" />
    <approval v-model="dialogApprovalVisible" :data="approvalForm" @change="hdlList" />
    <meal v-model="dialogStatusVisible" :data="statusForm" @change="hdlList" />
    <edit v-model="dialogEditVisible" :data="editForm" @change="hdlList" />
    <edit2 v-model="dialogEdit2Visible" :data="edit2Form" @change="hdlList" />
    <single-meal v-model="dialogSingleMealVisible" :data="singleMealForm" @change="hdlList" />

    <!-- 工牌生成 -->
    <u-dialog title="工牌生成" :show.sync="dialogBadgeVisible" width="410px" :footer="false">

      <div v-if="data.badgeVisible" ref="capture" class="badge">
        <span class="company">{{ data.company }}</span>
        <div class="avatar-placeholder">
          <img class="avatar" :src="data.avatarUrl" :fit="data.avatarfit" alt="头像">
        </div>
        <span class="name">{{ data.name }}</span>
        <img class="corner-image" :src="data.cornerImage" :fit="data.fit" alt="角落图片">
      </div>
      <el-button type="primary" style="margin-top: 20px;" @click="saveAsImage()">保存为图片</el-button>
    </u-dialog>
  </div>
</template>
<script>

import approval from './components/approval'
import modal from './components/modal'
import minorModal from './components/minorModal'
import meal from './components/meal.vue'
import singleMeal from './components/singleMeal'
import edit from './components/edit'
import edit2 from './components/edit2'
import MallFloor from '@/components/mall/floor.vue'
import html2canvas from 'html2canvas'

export default {
  components: { edit, edit2, meal, modal, singleMeal, approval, MallFloor, minorModal },
  data() {
    return {
      api: this.$u.api.PluginsGuide,
      // 表格
      columns: [
        {
          prop: 'type',
          label: '类型',
          width: 80,
          render: (h, params) => {
            const { type } = params.row
            let title = ''
            if (type === 0) {
              title = '正式员工'
            } else if (type === 1) {
              title = '试用员工'
            }
            return h('div', title)
          }
        },
        {
          prop: 'jobNo',
          label: '工号'
        },
        {
          prop: 'name',
          label: '姓名'
        },
        {
          prop: 'mobile',
          label: '手机号'
        },
        {
          prop: 'dept',
          label: '部门'
        },
        {
          prop: 'enterpriseName',
          label: '公司'
        },
        {
          prop: 'merchantName',
          label: '商户'
        },
        {
          prop: 'floor',
          label: '楼层'
        },
        {
          prop: 'mealCost',
          label: '餐费/元'
        },
        {
          prop: 'createTime',
          label: '入职时间'
        },
        {
          prop: 'status',
          label: '状态',
          render: (h, params) => {
            const { status } = params.row
            if (status === 0) {
              return h('el-tag', { props: { type: 'warning' }}, '待入职')
            } else if (status === 1) {
              return h('el-tag', { props: { type: 'success' }}, '正常')
            } else if (status === 2) {
              return h('el-tag', { props: { type: 'info' }}, '离职')
            } else if (status === 3) {
              return h('el-tag', { props: { type: 'danger' }}, '锁定')
            }
          }
        },
        {
          prop: 'lastLoginTime',
          label: '最后登录时间'
        },
        {
          prop: 'note',
          label: '备注',
          render: (h, params) => {
            const { lockNote, note } = params.row
            const title = lockNote ? `锁定原因:${lockNote},${note}` : note
            return h('span', title)
          }
        }
      ],

      list: [],
      listLoading: false,
      listOptions: {
        mutiSelect: true, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        beginTime: '',
        endTime: '',
        name: '',
        mobile: '',
        status: '',
        type: ''
      },

      dialogFormVisible: false,
      dialogMinorFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '查看详情',
        create: '新增导购'
      },
      purview: false,
      dataForm: {},
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: ['blur'] }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: ['blur'] }
        ]
      },
      // 删除选中数据
      selectData: [],
      active: 'first',
      dialogApprovalVisible: false,
      approvalForm: {},
      emergencyContactRelationList: [{
        value: '亲属',
        label: '亲属'
      },
      {
        value: '朋友',
        label: '朋友'
      },
      {
        value: '同事',
        label: '同事'
      },
      {
        value: '其他',
        label: '其他'
      }
      ],
      // ------------状态编辑-----------------
      dialogStatusVisible: false,
      statusForm: {},
      //    -----------------导购编辑--------------------
      dialogEditVisible: false,
      editForm: {},
      dialogEdit2Visible: false,
      edit2Form: {},
      // 单个餐费充值
      dialogSingleMealVisible: false,
      singleMealForm: {},

      dialogBadgeVisible: false,
      // 表格
      data: {
        avatarfit: '',
        cornerImagefit: '',
        name: '',
        company: '',
        cornerImage: 'https://csms.chiconysquare.net/assets/logo/brandlogo.png',
        badgeVisible: true,
        avatarUrl: ''
      }
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    /**
     * 获取列表数据
     */
    hdlList() {
      const self = this
      // 打开加载状态
      self.listLoading = true
      // 查询分页数据
      self.api.list(self.listQuery).then(res => {
        // 获取分页列表数据
        self.list = res.data.list
        // 获取分页数据,总页数
        self.listQuery.totalCount = res.data.totalCount
        self.purview = res.data.purview
        // 关闭加载状态
        setTimeout(() => {
          self.listLoading = false
        }, 0.5 * 1000)
      })
    },
    hdlSync(row) {
      const { id } = row
      const self = this
      // 模态对话框组件
      self.$confirm('此操作将同步企微数据是否继续？', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
        // 处理确认事件
        self.api.sync({ id }).then(() => {
          // 通知信息
          self.$u.msg('同步成功')
        })
      }).catch(() => {
      })
    },
    hdlEdit(row) {
      this.editForm = row
      this.dialogEditVisible = true
    },
    hdlEdit2(row) {
      this.edit2Form = row
      this.dialogEdit2Visible = true
    },
    hdlDetail(row) {
      this.dataForm = row
      this.dialogFormVisible = true
    },
    // 楼层查看详情
    hdlMinorDetail(row) {
      this.dataForm = row
      this.dialogMinorFormVisible = true
    },
    hdlUpdate(row) {
      this.statusForm = row
      this.dialogStatusVisible = true
    },
    hdlApproval(row) {
      this.approvalForm = row
      this.dialogApprovalVisible = true
    },
    hdlSingleMeal(row) {
      this.singleMealForm = row
      this.dialogSingleMealVisible = true
    },
    handleGuideDownload() {
      // 复制值
      const list = this.$u.deepClone(this.list)
      // 业务数据处理
      list.forEach(item => {
        this.$set(item, 'status', item.status === 0 ? '待入职' : item.status === 1 ? '正常' : item.status === 2 ? '离职' : item.status === 3 ? '锁定' : '')
      })
      // 表头&表数据
      const tHeader = ['工号', '姓名', '手机号', '公司', '商户', '楼层', '入职时间', '状态', '最后登录时间', '备注']
      const filterVal = ['jobNo', 'name', 'mobile', 'enterpriseName', 'merchantName', 'floor', 'createTime', 'status', 'lastLoginTime', 'note']
      const data = list.map(v => filterVal.map(j => { return v[j] }))
      this.$u.exports.excel('导购管理', tHeader, data)
    },
    handleDownload() {
      // 复制值
      const list = this.$u.deepClone(this.list)
      // 业务数据处理
      list.forEach(item => {
        this.$set(item, 'province', item.province + item.city + item.area + item.address)
        this.$set(item, 'status', item.status === 0 ? '待入职' : item.status === 1 ? '正常' : item.status === 2 ? '离职' : item.status === 3 ? '锁定' : '')
      })
      // 表头&表数据
      const tHeader = ['工号', '姓名', '民族', '手机', '籍贯', '生日', '身份证', '性别', '身份证有效期', '年龄', '户籍地址', '邮箱', '最高学历', '入职时间', '现住地址', '餐卡', '每月餐费标准', '状态', '备注', '离职时间', '离职事由', '紧急联络与本人关系', '联络人', '联络电话', '公司名称', '公司编码', '合同编号', '商户名称', '商户编码', '商户楼层', '身份证有效期', '健康证有效期']
      const filterVal = ['jobNo', 'name', 'nationality', 'mobile', 'bornPlace', 'birthday', 'idCard', 'sex', 'idCardValidity', 'age', 'bornAddress', 'email', 'highestDegree', 'createTime', 'province', 'mealCardNo', 'mealCost', 'status', 'note', 'endTime', 'endContent', 'emergencyContactRelation', 'emergencyName', 'emergencyMobile', 'enterpriseName', 'enterpriseNo', 'contractNo', 'merchantName', 'merchantNo', 'floor', 'idCardValidity', 'healthCertificateValidity']
      const data = list.map(v => filterVal.map(j => { return v[j] }))
      this.$u.exports.excel('导购管理', tHeader, data)
    },
    hdlBadge(row) {
      console.log(row)
      this.dialogBadgeVisible = true
      // this.data = row
      this.data.avatarfit = 'cover'
      this.data.cornerImagefit = ''
      this.data.name = row.name
      this.data.company = row.merchantName
      this.data.cornerImage = 'https://csms.chiconysquare.net/assets/logo/brandlogo.png'
      this.data.avatarUrl = 'https://csms.chiconysquare.net/' + row.avatar

      // this.data.avatarUrl = ' http://j.dsik.cn/' + row.avatar
      this.data.badgeVisible = true
    },
    saveAsImage() {
      const element = this.$refs.capture
      html2canvas(element).then((canvas) => {
        const link = document.createElement('a')
        link.href = canvas.toDataURL('image/png')
        link.download = 'badge-image.png'
        link.click()
      })
    },
    // 试用员工转正
    hdRegularize(row) {
      const self = this
      self.$confirm('此操作将同意申请转正，是否继续', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {

        self.api.regularizeEmployee(row).then(res => {
          // 通知信息
          self.$u.msg('操作成功')
        })
      }).catch(() => {
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .app-container{

    ::v-deep {
      .el-descriptions__title {
        font-size: 14px;
        font-weight: bold;
      }
    }

    & >>> .el-drawer__body {
      overflow: auto;
    }

    .uploader{

      .thumb{
        display: inline-block;
        text-align: center;
        outline: none;
        background-color: #fbfdff;
        border: 1px dashed #c0ccda;
        border-radius: 6px;
        box-sizing: border-box;
        width: 120px;
        height: 120px;
        cursor: pointer;
        line-height: 120px;
        vertical-align: top;

        ::v-deep {
          .el-image {
            width: 100%;
            height: 100%;
            display: block;
            border-radius: 6px;
          }

          img{
            width: 100%;
            height: 100%;
            display: block;
            border-radius: 6px;
          }
        }
      }
    }

    .badge {
      border: 1px solid #000;
      border-radius: 5px;
      width: 371px;
      height: 602px;
      //padding: 20px;
      text-align: center;
      margin-top: 20px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      position: relative;
    }
    .company {
      font-weight: bold;
      margin-top: 50px; /* 下移50px */
      display: -webkit-box; /* 使其成为弹性盒子 */
      -webkit-box-orient: vertical; /* 设置主轴方向为纵向 */
      -webkit-line-clamp: 2; /* 限制显示的行数为 2 */
      overflow: hidden; /* 隐藏多余内容 */
      transition: font-size 0.3s ease; /* 字体大小变化的过渡效果 */
      font-size: 40px; /* 默认字体大小 */
      color: black;
    }

    .name {
      font-size: 55px; /* 放大一倍 */
      font-weight: bold;
      color: black;
    }
    .avatar-placeholder {
      width: 245px; /* 椭圆形宽度 */
      height: 280px; /* 椭圆形高度 */
      border: 2px solid #000;
      border-radius: 50%; /* 椭圆形 */
      margin: 20px auto;
      position: relative;
      overflow: hidden;
    }
    .avatar {
      width: 100%;
      height: 100%;
      object-fit: cover;
      position: absolute;
      top: 0;
      left: 0;
    }
    .corner-image {
      position: absolute;
      width: 147px;
      height: 49px;
      top: 20px;
      right: 20px;
      object-fit: cover; /* 确保图像适应框 */
    }

  }
</style>
